﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;


namespace PayloadTracker
{
    [Serializable]
    public class Coordinate
    {
        public enum DistanceType
        {
            Miles,
            NauticalMiles,
            Kilometers, 
            Radian
            
        }
        public static readonly Coordinate EMPTY = new Coordinate();


        [XmlAttribute]
        public double Lat;
        [XmlAttribute]
        public double Long;

        public double LatRadians()
        {
            return Lat * Math.Abs(Math.PI/180);
        }
        public double LongRadians()
        {
            return Math.Abs(Long * Math.PI / 180);
        }
                
        public Coordinate()
        {
            Long = Lat = double.MinValue;
        }
        public Coordinate(double Lat, double Long)
        {
            this.Lat = Lat;
            this.Long = Long;
        }
        public override string ToString()
        {
            return "{Lat: " + Lat.ToString() + " Long: " + Long.ToString() + "}";
        }

        public double DistanceTo(Coordinate c,DistanceType dt)
        {

            var t1 = Math.Sin(LatRadians()) * Math.Sin(c.LatRadians());
            var t2 = Math.Cos(LatRadians()) * Math.Cos(c.LatRadians());
            var t3 = Math.Cos(LongRadians() - c.LongRadians());
            var t4 = t2 * t3;
            var t5 = t1 + t4;
            var rad_dist = Math.Atan(-t5 / Math.Sqrt(-t5 * t5 + 1)) + 2 * Math.Atan(1);
            

            switch(dt)
            {
                case DistanceType.Radian:
                    return rad_dist;
                case DistanceType.NauticalMiles:
                    return rad_dist * 3437.73877;
                case DistanceType.Miles:
                    return rad_dist * 3437.74677 * 1.1508;
                case DistanceType.Kilometers:
                    return rad_dist * 3437.74677 * 1.1508 * 1.6093470878864446;
            }
            
            return 0;
        }
    }
}
